package actionHandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import palette.CanvasManager;
import palette.CanvasWindow;
import xml.*;

public class SaveHandler implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser(new File(System.getProperty("user.dir")));
	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
	        "XML Documents", "xml");
	    chooser.setFileFilter(filter);
	    if(chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
	    	CanvasWindow temp = (CanvasWindow)(CanvasManager.getInst().getSelectedFrame());
	    	(new XmlWriter()).write(chooser.getName(chooser.getSelectedFile()));
	    	temp.setTitle(chooser.getName(chooser.getSelectedFile()));
	    }
	}

}